package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.dic.utils.DicParamItemUtils;
import com.fzrskj.cloud.free.sys.bean.SysMiniServiceBean;
import com.fzrskj.cloud.free.sys.utils.SysMiniServiceUtils;
import com.fzrskj.cloud.free.sys.bean.SysProductDiycatBean;
import com.fzrskj.cloud.free.sys.utils.SysProductDiycatUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SysDiyCatService.
 *
 * @author zj.
 *         Created on 2019/1/11 0011.
 */
public class SysDiyCatService extends CObject {
    private String formatDiyCat(SysProductDiycatBean obj){
        String ret = JsonTool.beanToJson(obj);
        if(obj != null && null != obj.getMiniServiceId()  && obj.getMiniServiceId() > 0){
            SysMiniServiceUtils miniServiceUtils = new SysMiniServiceUtils();
            SysMiniServiceBean miniServiceBean = miniServiceUtils.querySysMiniService(obj.getMiniServiceId());
            ret = JsonTool.addNode(ret, "serviceCode", miniServiceBean.getServiceCode());
        }
        return ret;
    }

    private String formatDiyCat(List<SysProductDiycatBean> objs){
        String rets = "[";
        for (SysProductDiycatBean obj: objs){
            rets += formatDiyCat(obj)+",";
        }
        if (objs.size()>0) rets = StrTool.deleteCharAt(rets, rets.length()-1);
        rets += "]";
        return rets;
    }

    /**
     * 查询自定义分类.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String queryDiyProCatList(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        int page =CJsonTool.getIntValue(p, "page", 0);
        int pageSize =CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);

        Map<String, Object> map = new HashMap<>();

        SysProductDiycatUtils obj = new SysProductDiycatUtils();
        int rows = obj.querySysProductDiycat(map);
        String code = obj.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)){
            return XmlCodeParser.formatResultTips(false,code);
        }

        if (rows<=0){
            return XmlCodeParser.formatResultTips(true,SysErr.SYS_DATA_NOT_FOUND);
        }

        if (rows<(page-1)*pageSize){
            return XmlCodeParser.formatResultTips(false,code);
        }
        int pages = Math.round((rows+pageSize-1)/pageSize);
        List<SysProductDiycatBean> diycatBeans = obj.querySysProductDiycat(map, " sortNum ", page, pageSize);
        code = obj.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }
        return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, rows, formatDiyCat(diycatBeans));
    }

    /**
     * 新增分类.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String newDiyProCat(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        String categoryCode = JsonTool.getStrValue(p, "categoryCode", "");
        String categoryName = JsonTool.getStrValue(p, "categoryName", "");
        Integer sortNum = JsonTool.getIntValue(p, "sortNum", 0);
        String diyPage = JsonTool.getStrValue(p, "diyPage", "");
        Integer diyHeight = JsonTool.getIntValue(p, "diyHeight", 0);
        String userDiyPage = JsonTool.getStrValue(p, "userDiyPage", "");
        String adminDiyPage = JsonTool.getStrValue(p, "adminDiyPage", "");
        Integer miniServiceId = JsonTool.getIntValue(p, "miniServiceId", 0);
        if (StrTool.isBlankStr(categoryCode) || StrTool.isBlankStr(categoryName)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        //检查是否已存在
        SysProductDiycatUtils diycatUtils = new SysProductDiycatUtils();
        Map<String, Object> existRule = new HashedMap();
        existRule.put("categoryCode", categoryCode);
        SysProductDiycatBean exist = diycatUtils.getSysProductDiycat(existRule);
        if (null!=exist) {
            return XmlCodeParser.formatResultTips(false, "CATEGORY EXISTED", "相同编码的分类已存在");
        }

        Map<String, Object> newData = new HashedMap();
        newData.put("categoryCode", categoryCode);
        newData.put("categoryName", categoryName);
        newData.put("sortNum", sortNum);
        newData.put("diyPage", diyPage);
        newData.put("diyHeight", diyHeight);
        newData.put("userDiyPage",userDiyPage);
        newData.put("adminDiyPage",adminDiyPage);
        newData.put("miniServiceId",miniServiceId);
        Integer ret = diycatUtils.newSysProductDiycat(newData);
        boolean success = (ret > 0);
        String code = diycatUtils.getErrCode();
        return XmlCodeParser.formatResultTips(success, code);
    }

    /**
     * 修改状态.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String editDiyProCatStatus(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        Integer id = JsonTool.getIntValue(p, "id", -1);
        Integer status = JsonTool.getIntValue(p, "status", -1);
        if (id < 1 || status < 0) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        Map<String, Object> cdt = new HashedMap();
        cdt.put("diyId", id);
        Map<String, Object> update = new HashedMap();
        update.put("status", status);
        SysProductDiycatUtils diycatUtils = new SysProductDiycatUtils();
        diycatUtils.modSysProductDiycat(update, cdt);
        String code = diycatUtils.getErrCode();
        Boolean success = SysErr.CODE_SUCCESS.equals(code);
        return XmlCodeParser.formatResultTips(success, code);
    }

    /**
     * 修改产品分类.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String editDiyProCat(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        Integer id = JsonTool.getIntValue(p, "id", -1);
        String categoryName = JsonTool.getStrValue(p, "categoryName", "");
        Integer sortNum = JsonTool.getIntValue(p, "sortNum", -1);
        String diyPage = JsonTool.getStrValue(p, "diyPage", "");
        Integer diyHeight = JsonTool.getIntValue(p, "diyHeight", 0);
        String userDiyPage = JsonTool.getStrValue(p, "userDiyPage", "");
        String adminDiyPage = JsonTool.getStrValue(p, "adminDiyPage", "");
        Integer miniServiceId = JsonTool.getIntValue(p, "miniServiceId", 0);
        if (id < 1 || sortNum < 0 || StrTool.isBlankStr(categoryName)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        Map<String, Object> cdt = new HashedMap();
        cdt.put("diyId", id);
        Map<String, Object> update = new HashedMap();
        update.put("categoryName", categoryName);
        update.put("sortNum", sortNum);
        update.put("diyPage", diyPage);
        update.put("diyHeight", diyHeight);
        update.put("userDiyPage",userDiyPage);
        update.put("adminDiyPage",adminDiyPage);
        update.put("miniServiceId",miniServiceId);
        SysProductDiycatUtils diycatUtils = new SysProductDiycatUtils();
        diycatUtils.modSysProductDiycat(update, cdt);
        String code = diycatUtils.getErrCode();
        Boolean success = SysErr.CODE_SUCCESS.equals(code);
        return XmlCodeParser.formatResultTips(success, code);
    }

    /**
     * 删除产品分类.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String deleteDiyProCat(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        Integer id = JsonTool.getIntValue(p, "id", -1);
        if (id < 1) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        SysProductDiycatUtils diycatUtils = new SysProductDiycatUtils();
        diycatUtils.delSysProductDiycat(id);
        String code = diycatUtils.getErrCode();
        Boolean success = SysErr.CODE_SUCCESS.equals(code);
        return XmlCodeParser.formatResultTips(success, code);
    }

    /**
     * 获取可用分类.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String usableDiyProCat(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        SysProductDiycatUtils obj = new SysProductDiycatUtils();
        List<SysProductDiycatBean> diycatBeans = obj.queryUsableList();
        String result = XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, formatDiyCat(diycatBeans));
        String diyServerName = DicParamItemUtils.getDescriptionByValue("diy");
        result = JsonTool.addNode(result, "diyName", diyServerName);
        return result;
    }

    /**
     * 获取所有分类列表.
     */
    public String allDiyProCat (HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        SysProductDiycatUtils obj = new SysProductDiycatUtils();
        List<SysProductDiycatBean> diycatBeans = obj.queryAllList();
        String result = XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, formatDiyCat(diycatBeans));
        String diyServerName = DicParamItemUtils.getDescriptionByValue("diy");
        result = JsonTool.addNode(result, "diyName", diyServerName);
        return result;
    }
}
